var gulp = require('gulp');
var folder = {
    src: 'src/',
    dist: 'dist/'
}

//环境变量 
//判断环境变量
//设置环境变量  process.env.NODE_ENV
var devMod = process.env.NODE_ENV == 'development';
console.log(devMod)



//插件  ==下载插件然后取到插件 在应用插件
//html
var htmlclean = require('gulp-htmlclean');

//图片
var imagemin = require('gulp-imagemin');

//js
var uglify = require('gulp-uglify');

//去除 调试语句
// var stripDebug = require('gulp-strip-debug');


//less 转成css  
var less = require('gulp-less');


//压缩css
var clean = require('gulp-clean-css');

//添加前缀 css3   插件：postcss autoprefixer
// var postcss = require('gulp-postcss')
// var autoprefixer = require('gulp-autoprefixer')


//开启服务器
var gulpConnect = require('gulp-connect');
gulp.task('html', function () {
    //取
    var page = gulp.src(folder.src + 'html/*')
        // html 发生变化 就触发服务器的loade
        .pipe(gulpConnect.reload());
    if (!devMod) {
        page.pipe(htmlclean())
    }
    //发送  传输
    page.pipe(gulp.dest(folder.dist + 'html/'))
})
gulp.task('css', function () {
    var page = gulp.src(folder.src + 'css/*')
        //gulpConnect.reload() ==是服务器的里面的方法 文件发生了改变就触发
        .pipe(gulpConnect.reload())
        .pipe(less())
    if (!devMod) {
        //判断他是开发环境 还是上线 开发就不压缩
        page.pipe(clean())
    }
    page.pipe(gulp.dest(folder.dist + 'css/'))
})
gulp.task('js', function () {
    gulp.src(folder.src + 'js/*')
        .pipe(gulpConnect.reload())
        // .pipe(uglify())
        // pipe(postcss([autoprefixer()]))
        .pipe(gulp.dest(folder.dist + 'js/'))
})

gulp.task('server', function () {
    gulpConnect.server({
        //改变端口
        port: '8888',
        //自动刷新
        livereload: true
    })
})
gulp.task('images', function () {
    gulp.src(folder.src + 'images/*')
        .pipe(imagemin())
        .pipe(gulp.dest(folder.dist + 'images'))

})
//监听
gulp.task('watch', function () {
    //watch  开启了对这个几个文件监听  就会执行后面的任务
    gulp.watch(folder.src + 'html/*', ['html'])
    gulp.watch(folder.src + 'css/*', ['css'])
    gulp.watch(folder.src + 'js/*', ['js'])
    gulp.watch(folder.src + "images/*", ["images"]);
})
gulp.task('default', ['html', 'css', 'js', 'images', 'server', 'watch'], function () {

});

//在git 怎么设置环境变量
//export NODE_ENV = development

//gulp  ==任务运行器
//webpack == 打包器